package cn.mesmile.lock.model.packet.controller;

import cn.mesmile.lock.aop.LockAction;
import cn.mesmile.lock.model.packet.entity.RedPacketInfo;
import cn.mesmile.lock.model.packet.service.RedPacketInfoService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author zb
 * @date 2020/12/26 17:07
 * @Description
 */
@RestController
@RequestMapping("/api/v1/redPacket")
public class RedPackController {

    private final RedPacketInfoService redPacketInfoService;

    private final RedisTemplate<String,Object> redisTemplate;

    public RedPackController(RedPacketInfoService redPacketInfoService, RedisTemplate<String, Object> redisTemplate) {
        this.redPacketInfoService = redPacketInfoService;
        this.redisTemplate = redisTemplate;
    }

    /**
     *  创建红包
     */
    @GetMapping("/create")
    public Boolean create(){
        RedPacketInfo redPacketInfo = new RedPacketInfo();
        long redPacketId = System.currentTimeMillis();
        redPacketInfo.setRedPacketId(redPacketId);

        redPacketInfo.setUid(12345678L);

        BigDecimal money = BigDecimal.valueOf(1000.88);

        redPacketInfo.setTotalAmount(money);
        redPacketInfo.setTotalPacket(9);

        redPacketInfo.setRemainingPacket(9);
        redPacketInfo.setRemainingAmount(money);

        boolean save = redPacketInfoService.save(redPacketInfo);
        if (save){
           redisTemplate.opsForValue().set("money:"+redPacketId,money.toString());
           redisTemplate.opsForValue().set("count:"+redPacketId,9);
        }
        return true;
    }


    /**
     * 抢红包
     */
    @LockAction(value = "red:packet")
    @GetMapping("/get")
    public void get(){

    }


    /**
     * 拆红包
     */
    @LockAction(value = "get:packet")
    @GetMapping("/getMoney")
    public void getMoney(){

    }


    /**
     * 红包记录
     */
    @GetMapping("/history")
    public void history(){

    }



}
